package com.lp.controller.iot;

import com.lp.bo.IotAlarmInfoBO;
import com.lp.bo.IotNodeInfoBO;
import com.lp.bo.IotSceneInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.common.RequestURL;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.DateUtils;
import com.lp.util.ResultMapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大屏统计
 */
@Controller
public class IotScreenController extends BaseController {
    /**
     * 查询所有项目（大屏统计）
     *
     * @param response
     * @param userKey
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.SCENE_INFO_ALL)
    public ModelAndView selectAll(HttpServletResponse response,
                                  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                  @RequestBody IotSceneInfoBO obj) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = service.selectList("IotSceneInfo.selectScreenAll", obj);
        } catch (Exception e) {
            exception(e, resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }


    /**
     * 查询所有设备（大屏统计）
     *
     * @param response
     * @param userKey
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.NodeInfo.NODE_INFO_ALL)
    public ModelAndView selectAll(HttpServletResponse response,
                                  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                  @RequestBody IotNodeInfoBO obj) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = service.selectList("IotNodeInfo.selectAll", obj);
        } catch (Exception e) {
            exception(e, resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }




    /**
     * 查询所有设备（大屏地图统计）
     *
     * @param response
     * @param userKey
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.NodeInfo.NODE_INFO_MAP)
    public ModelAndView selectMap(HttpServletResponse response,
                                  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                  @RequestBody IotNodeInfoBO obj) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = service.selectList("IotNodeInfo.selectMap", obj);
        } catch (Exception e) {
            exception(e, resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 统计用户（大屏）
     *
     * @param response
     * @param userKey
     * @param userInfoBO
     * @return 角色--用户数量
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURL.User.USER_SCREEN)
    public ModelAndView selectUserScreen(HttpServletResponse response,
                                         @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                         @RequestBody UserInfoBO userInfoBO) {
        Map<String, Object> resultMap = getResultMap();
        try {
            resultMap = service.selectList("UserInfo.selectUserScreen", userInfoBO);
        } catch (Exception e) {
            exception(e, resultMap, userInfoBO);
        }
        return getModelAndView(response, resultMap);
    }

    /**
     * 统计历史数据和触发历史 数量
     *
     * @param response
     * @param userKey
     * @param iotSceneInfoBO
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.SCENE_TRIGGER_SENSOR)
    public ModelAndView selectTriggerSensor(HttpServletResponse response,
                                            @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                            @RequestBody IotSceneInfoBO iotSceneInfoBO) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            iotSceneInfoBO.setUser_id(user.getId());
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            DateUtils dateUtils = new DateUtils();
            ArrayList<String> days = dateUtils.getDays(7);
            for (String day : days) {
                Map<String, Object> map = new HashMap<>();
                iotSceneInfoBO.setStart_time(day + " 00:00:00");
                iotSceneInfoBO.setEnd_time(day + " 23:59:59");
                map = service.selectOne("IotSceneInfo.selectTriggerSensor", iotSceneInfoBO);
                map.put("name", day);
                list.add(map);
            }
            resultMap.put("data", list);
        } catch (Exception e) {
            exception(e, resultMap, iotSceneInfoBO);
        }
        return getModelAndView(response, resultMap);
    }


    /**
     * 查询所有设备（大屏统计）
     *
     * @param response
     * @param userKey
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.NodeInfo.NODE_GUARANTEE)
    public ModelAndView guaranteeNode(HttpServletResponse response,
                                      @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                      @RequestBody IotNodeInfoBO obj) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            Map<String, Object> overInsurance = service.selectList("IotNodeInfo.overInsurance", obj);
            resultMap.put("over", overInsurance.get("data"));
            String futureDate = DateUtils.getFutureDate(30);
//            obj.setMaintenance_time(futureDate);
            Map<String, Object> preInsurance = service.selectList("IotNodeInfo.preInsurance", obj);
            resultMap.put("pre", preInsurance.get("data"));
        } catch (Exception e) {
            exception(e, resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }

}
